<?php

class wms_mdl_delivery_outerlogi extends dbeav_model{

    //是否有导出配置
    var $has_export_cnf = true;
    public $export_name = 'wms发货单';
    private $shipping_log = true;
    public $import_data  = array();
    public $logi_no_list = array();
    public $delivery_bn_array = array();

    public function table_name($real=false)
    {
        $table_name = 'delivery';
        if($real){
            return DB_PREFIX.'wms_'.$table_name;
        }else{
            return $table_name;
        }
    }

    public function exportName(&$data)
    {
        $data['name'] = '快速发货模板'.date('Ymd');
    }

    #导出快速发货模板
    function exportTemplate($filter){
        foreach ($this->io_title($filter) as $v){
            $title[] = kernel::single('base_charset')->utf2local($v);
        }
        return $title;
    }
    public function io_title( $filter=null,$ioType='csv'){
        switch( $ioType ){
            case 'csv':
            default:
                $this->oSchema['csv']['main'] = array(
                    // '*:发货单号' => 'delivery_bn',
                    '*:物流单号' => 'logi_no',
                    // '*:重量' => 'weight',
                    '*:物流公司' => 'logi_name',
                    // '*:收件人' => 'ship_name'
                );
        }
       $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType]['main'] );
        return $this->ioTitle[$ioType][$filter];
    }

    public function fgetlist_csv(&$data,$filter,$offset,$exportType = 1)
    {
        if($filter['isSelectedAll']){

            $new_filter = Array
            (
                'type' => 'normal',
                'pause' => "FALSE",
                'parent_id' => 0,
                'disabled' => "false",
                'status' => array('ready','progress'),
                'ext_branch_id' => $filter['ext_branch_id']
            );
        }else{
            $filter['process'] = 'false';
            $new_filter = $filter;
        }

        $title = array();
        if ( !$data['title'] ) {
            foreach( $this->io_title() as $k => $v ){
                $title[] = $v;
            }
            $data['title'] = '"'.implode('","',$title).'"';

            return true;
        }



        $list = $this->getList('delivery_bn,logi_name,ship_name',$new_filter,0,-1);

        if ($list) {
            $contents = array();
            foreach ($list as $v) {
                $contents = array(
                    // 'delivery_bn' => $v['delivery_bn']."\t",
                    'logi_no'     => '',
                    // 'weight'      => '',
                    'logi_name' => $v['logi_name'],
                    // 'ship_name' => $v['ship_name'],
                );
                $data['contents'][] = implode(',', $contents);
            }
        }
        return false;
    }
    public function getOuterData($filter){
        if($filter['isSelectedAll']){
            $new_filter = Array(
                    'type' => 'normal',
                    'pause' => "FALSE",
                    'parent_id' => 0,
                    'disabled' => "false",
                    'status' => array('ready','progress'),
                    'ext_branch_id' => $filter['ext_branch_id']
            );

            #第三方发货_加入固定条件 ExBOY
            $oBranch = app::get('ome')->model('branch');

            //所有第三方仓
            $outerBranch = array();
            $tmpBranchList = $oBranch->getList('branch_id',array('owner'=>'2'));
            foreach ($tmpBranchList as $key => $value) {
                $outerBranch[] = $value['branch_id'];
            }
            unset($tmpBranchList);

            //获取操作员管辖仓库
            $is_super = kernel::single('desktop_user')->is_super();
            if (!$is_super) {
                $branch_ids = $oBranch->getBranchByUser(true);
                if ($branch_ids) {
                    $new_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                    $new_filter['ext_branch_id'] = array_intersect($new_filter['ext_branch_id'], $outerBranch);
                } else {
                    $new_filter['ext_branch_id'] = 'false';
                }
            } else {
                $new_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $outerBranch;#优先_获取选择的仓库 ExBOY
            }

            #商品种类
            if(isset($_POST['skuNum']) && isset($_POST['_skuNum_search']))
            {
                $new_filter['skuNum|' . $_POST['_skuNum_search']]    = intval($_POST['skuNum']);
            }

            #商品总数量
            if(isset($_POST['itemNum']) && isset($_POST['_itemNum_search']))
            {
                $new_filter['itemNum|' . $_POST['_itemNum_search']]    = intval($_POST['itemNum']);
            }

            #发货单分组
            if($_POST['delivery_group'])
            {
                $new_filter['delivery_group']    = $_POST['delivery_group'];
            }

            #来源店铺
            if($_POST['shop_id'])
            {
                $new_filter['shop_id']    = $_POST['shop_id'];
            }

            #货号
            if($_POST['product_bn'])
            {
                $itemsObj    = $this->app->model("delivery_items");
                $rows        = $itemsObj->getDeliveryIdByPbn($filter['product_bn']);

                if($rows)
                {
                    $deliveryId_list    = array();
                    foreach($rows as $row){
                        $deliveryId_list[]    = $row['delivery_id'];
                    }
                    $new_filter['delivery_id']    = $deliveryId_list;
                }
            }
        }else{
            $filter['process'] = 'false';
            $new_filter = $filter;
        }
        $list = $this->getList('delivery_bn,logi_name,ship_name',$new_filter,0,-1);
        if ($list) {
            $contents = array();
            foreach ($list as $v) {
                $logino = '';
                $wmsDeliveryId = app::get('wms')->model('delivery')->dump(array('delivery_bn'=>$v['delivery_bn']),'delivery_id');
                $logiNo = app::get('wms')->model('delivery_bill')->dump(array('delivery_id'=>$wmsDeliveryId['delivery_id']),'logi_no');
                if(isset($logiNo['logi_no']) && !empty($logiNo['logi_no'])){
                    $logino = $logiNo['logi_no'];
                }
                $contents = array(
                        // 'delivery_bn' => $v['delivery_bn']."\t",
                        'logi_no'     => $logino,
                        // 'weight'      => '',
                        'logi_name' => $v['logi_name'],
                        // 'ship_name' => $v['ship_name'],
                );
               $data['content'][] =  kernel::single('base_charset')->utf2local('"'.implode( '","', $contents ).'"');
            }
        }
        return $data;
    }


    public function prepared_import_csv(){
        $this->ioObj->cacheTime = time();
    }

    public function prepared_import_csv_row($row,&$title,&$tmpl,&$mark,&$newObjFlag,&$msg)
    {
        if(empty($row)) return false;

        if( substr($row[0],0,1) == '*' ){
            $this->nums = 1;
            $title = array_flip($row);

            $this->logi_no_list    = array();
            $this->delivery_bn_array     = array();

            # $mark = 'title';
            return false;
        }

        if (empty($title)) {
            $msg['error'] = "请使用正确的模板格式！";
            return false;
        }

        // $delivery_bn = trim($row[$title['*:发货单号']]);
        //$outer_delivery_bn = $row[$title['*:外部发货单号']];
        //$outer_supplier = $row[$title['*:承接商']];
        $logi_no = trim($row[$title['*:物流单号']]);
        //$delivery_cost_actual = $row[$title['*:实际物流费用']];
        // $weight = trim($row[$title['*:重量']]);

        if(isset($this->nums)){
            $this->nums++;
            if($this->nums > 5000){
                $msg['error'] = "导入的数据量过大，请减少到5000条以下！";
                $this->shipping_log = false;
                return false;
            }
        }

        #$mark = 'contents';
        if (empty($logi_no)) {
            $msg['warning'][] = 'Line '.$this->nums.'：运单号不能都为空！';
            $this->shipping_log = false;
            return false;
        }

        // if (empty($delivery_bn)) {
        //     $msg['warning'][] = 'Line '.$this->nums.'：发货单号不能都为空！';
        //     $this->shipping_log = false;
        //     return false;
        // }

        # 获取第三方发货仓
        // $branchList = app::get('ome')->model('branch')->getList('branch_id',array('owner'=>'2'));
        // $branchIds = array();
        // foreach ($branchList as $key => $value) {
        //     $branchIds[] = $value['branch_id'];
        // }
        //if (empty($branchIds)) {
        //    $msg['error'] = '第三方仓不存在，请新建！！！';
        //    $this->shipping_log = false;
        //    return true;
        //}

        # 判断发货单是否存在
        $wmsdeliveryBill = app::get('wms')->model('delivery_bill');
        $delivery_id = $wmsdeliveryBill->dump(array('logi_no'=>$logi_no),'delivery_id');
        $deliModel = app::get('wms')->model('delivery');
        $delivery = $deliModel->dump(array('delivery_id'=>$delivery_id['delivery_id']),'delivery_id,delivery_bn,status,logi_id,logi_name,branch_id,net_weight,weight,ship_area');
        if (!$delivery) {
            $msg['warning'][] = 'Line '.$this->nums.'：未找到物流单号【'. $logi_no .'】中发货单数据！';
            $this->shipping_log = false;
            return false;
        }

        # 验证运单号是否被使用过
        $dlyCheckLib = kernel::single('wms_delivery_check');
        $logi_no_exist = $dlyCheckLib->existExpressNoBill($logi_no,$delivery['delivery_id']);
        if ($logi_no_exist) {
            $msg['warning'][] = 'Line '.$this->nums.'：运单号已被使用！';
            $this->shipping_log = false;
            return false;
        }

        #判断本次导入是否有重复的物流单号 ExBOY
        if(in_array($logi_no, $this->logi_no_list))
        {
            $msg['warning'][] = 'Line '.$this->nums.'：物流单号【'. $logi_no .'】重复！';
            $this->shipping_log = false;
            return false;
        }
        #判断发货单号是否暂停或退款状态
        if ($delivery['status'] == 2) {
            $msg['warning'][] = 'Line '.$this->nums.'：物流单号【'. $logi_no .'】中发货单已暂停！请修改后重新导入';
            $this->shipping_log = false;
            return false;
        }
        #判断本次导入是否有重复的发货单号 ExBOY
        // if(in_array($delivery_bn, $this->delivery_bn_array))
        // {
        //     $msg['warning'][] = 'Line '.$this->nums.'：发货单号【'. $delivery_bn .'】重复！';
        //     $this->shipping_log = false;
        //     return false;
        // }

        //判断是否为重复csv导入操作
        $result    = kernel::database()->select('select filedata from sdb_wms_third_shipping order by create_time desc limit 1');
        if($result){
           $filedata = json_decode($result[0]['filedata']);
           $list = array();
            foreach($filedata as $val) {
                $list[] = $val[0];
            }
            if(in_array($logi_no,$list)){
                 $msg['warning'][] = '模板数据不能重复多次导入';
                 return false;
            }
        }
        // $this->delivery_bn_array[] = $delivery_bn;
        $this->logi_no_list[]    = $logi_no;

        if ($delivery['status'] == 3) {
            $msg['warning'][] = 'Line '.$this->nums.'：物流单号【'. $logi_no .'】中发货单已发货！不能重复多次导入';
            $this->shipping_log = false;
            return false;
        }
        //检测导入物流单号是否发生了改变
        // $deliveryBillObj = app::get('wms')->model('delivery_bill');
        // $logi = $deliveryBillObj->getList('logi_no',array('delivery_id' =>$delivery['delivery_id'],'type'=>1));
        //  if(!empty($logi) && !empty($logi[0]['logi_no']) && ($logi[0]['logi_no'] != null)){
        //         #检测物流单号是否进行了修改
        //         if($logi[0]['logi_no'] != $logi_no){
        //             $msg['warning'][] = 'Line '.$this->nums.':模板快递单号与订单快递单不符';
        //             $this->shipping_log = false;
        //             return false;
        //         }
        //     }
        // if (!in_array($delivery['branch_id'], $branchIds)) {
        //     $msg['warning'][] = 'Line '.$this->nums.'：不是第三方仓不能发货！';
        //     return false;
        // }
        $logi_name = trim($row[$title['*:物流公司']]);
        if(!empty($logi_name)){
            #检测物流公司是否改变
            $filter['delivery_bn']  = $delivery['delivery_bn'];
            $filter['logi_name'] = $logi_name;
            $rs = $deliModel->getList('delivery_id',$filter);
            #当改变了物流公司,检测物流公司是否存在
            if(empty($rs)){
                #检测物流公司是否存在
                $obj_dly_corp = app::get('ome')->model('dly_corp');
                $corp_info = $obj_dly_corp->dump(array('name'=>$logi_name),'corp_id,name');
                if(empty($corp_info)){
                    $msg['warning'][] = 'Line '.$this->nums.'：物流公司有误！';
                    $this->shipping_log = false;
                    return false;
                }
            }
        }
         if(isset($delivery['logi_name']) && !empty($delivery['logi_name'])) {
            if($delivery['logi_name'] != $logi_name) {
                 $msg['warning'][] = 'Line '.$this->nums.':模板物流公司与订单物流公司不符';
                 $this->shipping_log = false;
                 return false;
            }
         }
        // 库存验证
        $branch_pObj = app::get('ome')->model('branch_product');
        $delivery_items = $this->app->model('delivery_items')->getList('*',array('delivery_id'=>$delivery[0]['delivery_id']));
        foreach ($delivery_items as $key=>$value) {
            $bp = $branch_pObj->dump(array('branch_id'=>$delivery['branch_id'],'product_id'=>$value['product_id']),'store');
            if ($bp['store'] < $value['number']) {
                $msg['warning'][] = 'Line '.$this->nums.'：【'.$value['product_name'].'】商品库存不足';
                $this->shipping_log = false;
                return false;
            }
        }

        // $minWeight = app::get('wms')->getConf('wms.delivery.minWeight');
        // if (empty($weight)) {
        //     $weight = $delivery['net_weight'] ? $delivery['net_weight'] : $minWeight;
        // }

        // $weightSet = app::get('wms')->getConf('wms.delivery.weight');
        // if (empty($weight) && $weightSet=='on'){
        //     $msg['warning'][] = 'Line '.$this->nums.'：请输入重量信息！';
        //     return false;
        // }

        // $maxWeight = app::get('wms')->getConf('wms.delivery.maxWeight');
        // if($weight < $minWeight || $weight > $maxWeight){
        //     $msg['warning'][] = 'Line '.$this->nums.'：包裹重量超出系统设置范围！';
        //     return false;
        // }
        //按照客户需求,第三方发货导入把包裹重量去掉,默认为订单的包裹重量
        $weight = $delivery['weight'];
        //获取物流费用
        $wmsCommonLib = kernel::single('wms_common');

        $area = $delivery['consignee']['area'];
        $arrArea = explode(':', $area);
        $area_id = $arrArea[2];
        $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id,$delivery['logi_id'],$weight);

        $opInfo = kernel::single('ome_func')->getDesktopUser();

        $sdf = array(
            'delivery_id' => $delivery['delivery_id'],
            //'outer_delivery_bn' => $outer_delivery_bn,
            //'outer_supplier' => $outer_supplier,
            'logi_no' => $logi_no,
            'delivery_cost_actual' => $delivery_cost_actual ? $delivery_cost_actual : 0,
            'weight' => $weight ? $weight : 0,
            'opInfo' => $opInfo,
            'is_super' => kernel::single('desktop_user')->is_super(),
            'user_data' => kernel::single('desktop_user')->user_data,
            'status'=>3,//发货状态(改为已发货)
            'process_status'=>7,
            'print_status'=>7,
            'delivery_time' => time(),//发货时间
            //'verify' => $delivery[0]['verify'],
            //'stock_status' => $delivery[0]['stock_status'],
            //'deliv_status' => $delivery[0]['deliv_status'],
            //'expre_status' => $delivery[0]['expre_status'],
        );
        if ($corp_info) {
            $sdf['logi_id'] = $corp_info['corp_id'];
            $sdf['logi_name'] = $corp_info['name'];
        }

        $this->import_data[] = $sdf;
        return true;
    }
    //上面的那个导入老是请求超时,就改为了这个
    public function prepared_import_csv_outerlogin($logi_no,$logi_name,$nums,&$msg)
    {
        if (empty($logi_no)) {
            $msg['warning'][] = 'Line '.$nums.'：运单号不能都为空！';
            $this->shipping_log = false;
            return false;
        }
        # 判断发货单是否存在
        $wmsdeliveryBill = app::get('wms')->model('delivery_bill');
        $delivery_id = $wmsdeliveryBill->dump(array('logi_no'=>$logi_no),'delivery_id');
        $deliModel = app::get('wms')->model('delivery');
        $delivery = $deliModel->dump(array('delivery_id'=>$delivery_id['delivery_id']),'delivery_id,delivery_bn,status,logi_id,logi_name,branch_id,net_weight,weight,ship_area');
        if (!$delivery) {
            $msg['warning'][] = 'Line '.$nums.'：未找到物流单号【'. $logi_no .'】中发货单数据！';
            $this->shipping_log = false;
            return false;
        }

        # 验证运单号是否被使用过
        $dlyCheckLib = kernel::single('wms_delivery_check');
        $logi_no_exist = $dlyCheckLib->existExpressNoBill($logi_no,$delivery['delivery_id']);
        if ($logi_no_exist) {
            $msg['warning'][] = 'Line '.$nums.'：运单号已被使用！';
            $this->shipping_log = false;
            return false;
        }

        #判断本次导入是否有重复的物流单号 ExBOY
        if(in_array($logi_no, $this->logi_no_list))
        {
            $msg['warning'][] = 'Line '.$nums.'：物流单号【'. $logi_no .'】重复！';
            $this->shipping_log = false;
            return false;
        }
        #判断发货单号是否暂停或退款状态
        if ($delivery['status'] == 2) {
            $msg['warning'][] = 'Line '.$nums.'：物流单号【'. $logi_no .'】中发货单已暂停！请修改后重新导入';
            $this->shipping_log = false;
            return false;
        }

        //判断是否为重复csv导入操作
        $result    = kernel::database()->select('select filedata from sdb_wms_third_shipping order by create_time desc limit 1');
        if($result){
           $filedata = json_decode($result[0]['filedata']);
           $list = array();
            foreach($filedata as $val) {
                if (gettype($val) == 'object') {
                    $val = json_decode( json_encode( $val),true);
                }
                $list[] = $val['logi_no'];
            }
            if(in_array($logi_no,$list)){
                 $msg['warning'][] = '模板数据不能重复多次导入';
                 return false;
            }
        }
        $this->logi_no_list[]    = $logi_no;

        if ($delivery['status'] == 3) {
            $msg['warning'][] = 'Line '.$nums.'：物流单号【'. $logi_no .'】中发货单已发货！不能重复多次导入';
            $this->shipping_log = false;
            return false;
        }

        if(!empty($logi_name)){
            #检测物流公司是否改变
            $filter['delivery_bn']  = $delivery['delivery_bn'];
            $filter['logi_name'] = $logi_name;
            $rs = $deliModel->getList('delivery_id',$filter);
            #当改变了物流公司,检测物流公司是否存在
            if(empty($rs)){
                #检测物流公司是否存在
                $obj_dly_corp = app::get('ome')->model('dly_corp');
                $corp_info = $obj_dly_corp->dump(array('name'=>$logi_name),'corp_id,name');
                if(empty($corp_info)){
                    $msg['warning'][] = 'Line '.$nums.'：物流公司有误！';
                    $this->shipping_log = false;
                    return false;
                }
            }
        }
         if(isset($delivery['logi_name']) && !empty($delivery['logi_name'])) {
            if($delivery['logi_name'] != $logi_name) {
                 $msg['warning'][] = 'Line '.$nums.':模板物流公司与订单物流公司不符';
                 $this->shipping_log = false;
                 return false;
            }
         }
        // 库存验证
        $branch_pObj = app::get('ome')->model('branch_product');
        $delivery_items = $this->app->model('delivery_items')->getList('*',array('delivery_id'=>$delivery[0]['delivery_id']));
        foreach ($delivery_items as $key=>$value) {
            $bp = $branch_pObj->dump(array('branch_id'=>$delivery['branch_id'],'product_id'=>$value['product_id']),'store');
            if ($bp['store'] < $value['number']) {
                $msg['warning'][] = 'Line '.$nums.'：【'.$value['product_name'].'】商品库存不足';
                $this->shipping_log = false;
                return false;
            }
        }
        //按照客户需求,第三方发货导入把包裹重量去掉,默认为订单的包裹重量
        $weight = $delivery['weight'];
        //获取物流费用
        $wmsCommonLib = kernel::single('wms_common');

        $area = $delivery['consignee']['area'];
        $arrArea = explode(':', $area);
        $area_id = $arrArea[2];
        $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id,$delivery['logi_id'],$weight);

        $opInfo = kernel::single('ome_func')->getDesktopUser();
        $sdf = array(
            'delivery_id' => $delivery['delivery_id'],
            'logi_no' => $logi_no,
            'delivery_cost_actual' => $delivery_cost_actual ? $delivery_cost_actual : 0,
            'weight' => $weight ? $weight : 0,
            'opInfo' => $opInfo,
            'is_super' => kernel::single('desktop_user')->is_super(),
            'user_data' => kernel::single('desktop_user')->user_data,
            'status'=>3,//发货状态(改为已发货)
            'process_status'=>7,
            'print_status'=>7,
            'delivery_time' => time(),//发货时间
        );
        if ($corp_info) {
            $sdf['logi_id'] = $corp_info['corp_id'];
            $sdf['logi_name'] = $corp_info['name'];
        }
        return $sdf;
        // $this->import_data[] = $sdf;
        // $this->finish_import_csv();
        // return true;
    }
    function prepared_import_csv_obj($data,$mark,$tmpl,&$msg = ''){
        return null;
    }

    public function finish_import_csv($import_data){

        $oQueue = app::get('base')->model('queue');
        // if($this->shipping_log == false){
        //     $this->import_data = array();
        // }
        $queueData = array(
            'queue_title'=>'外部运单号导入',
            'start_time'=>time(),
            'params'=>array(
                // 'sdfdata'=>$this->import_data,
                'sdfdata'=>$import_data,
                'app' => 'wms',
                'mdl' => 'delivery_outerlogi',
            ),
            'worker'=>'wms_mdl_delivery_outerlogi.import_run',
        );
        $oQueue->save($queueData);

        $oQueue->flush();
    }

    public function import_run($cursor_id,$params, &$errormsg)
    {
        $now = time();
        $opObj = app::get('ome')->model('operation_log');

        $dlyCheckLib = kernel::single('wms_delivery_check');
        $dlyProcessLib = kernel::single('wms_delivery_process');

        $deliveryObj = app::get('wms')->model('delivery');
        $deliveryBillObj = app::get('wms')->model('delivery_bill');
        $omeDeliveryBillObj = app::get('ome')->model('delivery');
        $wmsItemsObj = app::get('wms')->model('delivery_items');
        $logi_no_array = array();
        foreach ($params['sdfdata'] as $key=>$value) {
            $dlyInfo = $deliveryObj->dump($value['delivery_id'],'print_status,process_status,branch_id,outer_delivery_bn,logi_id,logi_name,memo,delivery_bn');
           $deliveryCount = $wmsItemsObj->getList('delivery_id',array('delivery_id'=>$value['delivery_id']));
           //是否更新物流公司
           if($value['logi_name'] && $value['logi_id']) {
             $deliveryObj->update(array('logi_name'=>$value['logi_name'],'logi_id'=>$value['logi_id']),array('delivery_id'=>$value['delivery_id']));
           }
           //更新打印状态为已打印
            $deliveryObj->update(array('is_threeship'=>true,'logistStatus'=>1,'embrace_time'=>$now),array('delivery_id'=>$value['delivery_id']));
            $opObj->write_log('delivery_modify@wms', $value['delivery_id'], '第三方发货，更改快递公司',$now,$opInfo);

            if($dlyInfo['print_status'] > 0){
                if (($dlyInfo['print_status'] & 1) != 1) {
                    $opObj->write_log('delivery_stock@wms', $value['delivery_id'], '备货单打印(系统模拟打印)',$now,$opInfo);
                }
                if (($dlyInfo['print_status'] & 2) != 2) {
                    $opObj->write_log('delivery_deliv@wms', $value['delivery_id'], '发货单打印(系统模拟打印)',$now,$opInfo);
                }
                if (($dlyInfo['print_status'] & 4) != 4) {
                    $opObj->write_log('delivery_expre@wms', $value['delivery_id'], '快递单打印(系统模拟打印)',$now,$opInfo);
                }
            }

                $logi_no_array[] = $value['logi_no'];
                $weight_array[] = $value['weight'];
                $opObj->write_log('delivery_modify@wms', $value['delivery_id'], '修改发货单详情');

                # 校验
                if (($value['process_status'] &  2) != 2) {
                    $delivery = $dlyCheckLib->checkAllow($value['logi_no'], $msg, 'batch');
                    if ($delivery === false) {
                        // $this->db->rollback();

                        #加入报错msg
                        $errormsg[]    = $msg . '(发货单: '. $dlyInfo['delivery_bn'] .'校验报错)';

                        continue;
                    }

                    $verify = $dlyProcessLib->verifyDelivery($value['delivery_id']);
                    if (!$verify) {
                        // $this->db->rollback();

                        #加入报错msg
                        $errormsg[]    = '发货单校验失败(发货单: '. $dlyInfo['delivery_bn'] .')';

                        continue;
                    }
                }

                # 发货
                $return_error = $dlyCheckLib->consignAllow('', $value['logi_no'], $value['weight']);
                if ($return_error) {
                    // $this->db->rollback();

                    #加入报错msg
                    $errormsg[]    = $return_error . '(发货单: '. $dlyInfo['delivery_bn'] .'发货报错)';

                    continue;
                }
        }
        $logi_str = implode(',',$logi_no_array);
        $pdatoomsShippda = kernel::single('wms_pdatooms_shippda');
        $pdatoomsShippda->PDATOOMSbatchCheckAndDelivery($logi_str);
        #汇总报错msg
        // $errormsg    = implode(',', $errormsg);

        // return false;
    }

    public function getBranchByOp($op_id)
    {
        $bps = array();
        $oBops = app::get('ome')->model('branch_ops');
        $bops_list = $oBops->getList('branch_id', array('op_id' => $op_id), 0, -1);
        if ($bops_list){
            $bps = array_map('current',$bops_list);
        }

        return $bps;
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){

        $deliveryObj = app::get('ome')->model("delivery");
        $wmsObj = app::get('wms')->model("delivery");
        if(isset($filter['extend_delivery_id'])){
            $where .= ' OR delivery_id IN ('.implode(',', $filter['extend_delivery_id']).')';
            unset($filter['extend_delivery_id']);
        }
        if (isset($filter['member_uname'])){
            $memberObj = app::get('ome')->model("members");
            $rows = $memberObj->getList('member_id',array('uname|has'=>$filter['member_uname']));
            $memberId[] = 0;
            foreach($rows as $row){
                $memberId[] = $row['member_id'];
            }
            $where .= '  AND member_id IN ('.implode(',', $memberId).')';
            unset($filter['member_uname']);
        }
        if (isset($filter['order_bn'])){
            $orderObj = app::get('ome')->model("orders");
            $rows = $orderObj->getList('order_id',array('order_bn'=>$filter['order_bn']));
            $orderId[] = 0;
            foreach($rows as $row){
                $orderId[] = $row['order_id'];
            }

            $deliOrderObj = app::get('ome')->model("delivery_order");
            $rows = $deliOrderObj->getList('delivery_id',array('order_id'=>$orderId));
            $deliveryId[] = 0;
            foreach($rows as $row){
                $deliveryId[] = $row['delivery_id'];
            }
            //

            $_delivery_bn = $wmsObj->_getdelivery_bn($deliveryId);

            $where .= '  AND outer_delivery_bn IN (\''.implode('\',\'', $_delivery_bn).'\')';
            unset($filter['order_bn']);
        }

        if(isset($filter['no_logi_no']) && $filter['no_logi_no'] == true){
            $rows = $this->db->select("select delivery_id from sdb_wms_delivery_bill where logi_no = '' or logi_no is null");
            $deliveryId[] = 0;
            foreach($rows as $row){
                $deliveryId[] = $row['delivery_id'];
            }
            $where .= '  AND delivery_id IN ('.implode(',', $deliveryId).')';
            unset($filter['no_logi_no']);
        }

        if(isset($filter['product_bn'])){
            $itemsObj = $this->app->model("delivery_items");
            $rows = $itemsObj->getDeliveryIdByPbn($filter['product_bn']);

            $deliveryId[] = 0;
            foreach($rows as $row){
                $deliveryId[] = $row['delivery_id'];
            }
            //$_delivery_bn = $this->_getdelivery_bn($deliveryId);
            $where .= '  AND delivery_id IN (\''.implode('\',\'', $deliveryId).'\')';
            unset($filter['product_bn'],$_delivery_bn);
        }
        if(isset($filter['product_barcode'])){
            $itemsObj = $this->app->model("delivery_items");
            $rows = $itemsObj->getDeliveryIdByPbarcode($filter['product_barcode']);
            $deliveryId[] = 0;
            foreach($rows as $row){
                $deliveryId[] = $row['delivery_id'];
            }
            //$_delivery_bn = $this->_getdelivery_bn($deliveryId);
            $where .= '  AND delivery_id IN (\''.implode('\',\'', $deliveryId).'\')';

            unset($filter['product_barcode'],$_delivery_bn);
        }
        if(isset($filter['logi_no_ext'])){
            $logObj = $this->app->model("delivery_bill");
            $rows = $logObj->getlist('delivery_id',array('logi_no'=>$filter['logi_no_ext']));
            $deliveryId[] = 0;
            foreach($rows as $row){
                $deliveryId[] = $row['delivery_id'];
            }
            $where .= '  AND delivery_id IN ('.implode(',', $deliveryId).')';
            unset($filter['logi_no_ext']);
        }
         if(isset($filter['addonSQL'])){
            $where .= ' AND '.$filter['addonSQL'];
            unset($filter['addonSQL']);
        }
        if(isset($filter['delivery_ident'])){
            $arr_delivery_ident = explode('_',$filter['delivery_ident']);
            $mdl_queue = app::get('ome')->model("print_queue");
            if(count($arr_delivery_ident) == 2){
                $ident_dly = array_pop($arr_delivery_ident);
                $ident = implode('-',$arr_delivery_ident);
                $queueItem = $mdl_queue->findQueueItem($ident,$ident_dly);
                if($queueItem){
                    $where .= '  AND delivery_id ='.$queueItem['delivery_id'].'';
                }else{
                    $where .= '  AND delivery_id IN (0)';
                }
            }else{
                $queue = $mdl_queue->findQueue($filter['delivery_ident'],'dly_bns');
                if($queue){
                    $where .= '  AND delivery_id IN ('.$queue['dly_bns'].')';
                }else{
                    $where .= '  AND delivery_id IN (0)';
                }
            }

            unset($filter['delivery_ident']);
        }
        if(isset($filter['ship_tel_mobile'])){
            $where .= ' AND (ship_tel=\''.$filter['ship_tel_mobile'].'\' or ship_mobile=\''.$filter['ship_tel_mobile'].'\')';
            unset($filter['ship_tel_mobile']);
        }
        if($filter['todo']==1){
            $where .= " AND ((print_status & 1) !=1 or (print_status & 2) !=2 or (print_status & 4) !=4)";
            unset($filter['todo']);
        }
        if($filter['todo']==2){
            $where .= " AND ((print_status & 1) !=1 or (print_status & 4) !=4)";
            unset($filter['todo']);
        }
        if($filter['todo']==3){
            $where .= " AND ((print_status & 2) !=2 or (print_status & 4) !=4)";
            unset($filter['todo']);
        }
        if($filter['todo']==4){
            $where .= " AND (print_status & 4) !=4";
            unset($filter['todo']);
        }

        if (isset($filter['print_finish'])) {
            $where_or = array();
            foreach((array)$filter['print_finish'] as $key=> $value){
                $or = "(deli_cfg='".$key."'";
                switch($value) {
                    case '1_1':
                        $or .= " AND (print_status & 1) =1 and (print_status & 2) =2 ";
                        break;
                    case '1_0':
                        $or .= " AND (print_status & 1) =1 ";
                        break;
                    case '0_1':
                        $or .= " AND (print_status & 2) =2 ";
                        break;
                    case '0_0':
                        break;
                }
                $or .= ')';
                $where_or[] = $or;
            }
            if($where_or){
                $where .= ' AND ('.implode(' OR ',$where_or).')';
            }
            unset($filter['print_finish']);
        }
        if (isset($filter['ext_branch_id'])) {
            if (isset($filter['branch_id'])){
                $filter['branch_id'] = array_intersect((array)$filter['branch_id'],(array)$filter['ext_branch_id']);
                $filter['branch_id'] = $filter['branch_id'] ? $filter['branch_id'] : 'false';
            }else{
                $filter['branch_id'] = $filter['ext_branch_id'];
            }

            unset($filter['ext_branch_id']);
        }
         #客服备注
        if(isset($filter['mark_text'])){
            $mark_text = $filter['mark_text'];
            $sql = "SELECT do.delivery_id FROM sdb_ome_delivery_order do JOIN sdb_ome_orders o ON do.order_id=o.order_id  and o.process_status='splited' and  o.mark_text like "."'%{$mark_text}%'";
            $_rows = $this->db->select($sql);
            if(!empty($_rows)){
                foreach($_rows as $_orders){
                    $_delivery[] = $_orders['delivery_id'];
                }
                $_delivery_bn = $wmsObj->_getdelivery_bn($_delivery);
                $where .= '  AND outer_delivery_bn IN (\''.implode('\',\'', $_delivery_bn).'\')';
                unset($filter['mark_text'],$_delivery,$_delivery_bn);
            }

        }
        #买家留言
        if(isset($filter['custom_mark'])){
            $custom_mark = $filter['custom_mark'];
            $sql = "SELECT do.delivery_id FROM sdb_ome_delivery_order do JOIN sdb_ome_orders o ON do.order_id=o.order_id  and o.process_status='splited' and  o.custom_mark like "."'%{$custom_mark}%'";
            $_rows = $this->db->select($sql);
            if(!empty($_rows)){
                foreach($_rows as $_orders){
                    $_delivery[] = $_orders['delivery_id'];
                }
                $_delivery_bn = $wmsObj->_getdelivery_bn($_delivery);

                $where .= '  AND outer_delivery_bn IN (\''.implode('\',\'', $_delivery_bn).'\')';
                unset($filter['custom_mark'],$_delivery,$_delivery_bn);
            }

        }
        if (isset($filter['stock_status'])) {
            if ($filter['stock_status'] == 'true') {
                $where .= " AND (print_status & 1) =1";
            }else{
                $where .= " AND (print_status & 1) !=1";
            }
            unset($filter['stock_status']);
        }
        if (isset($filter['deliv_status'])) {
            if ($filter['deliv_status']=='true') {
                $where .= " AND (print_status & 2) =2";
            }else{
                $where .= " AND (print_status & 2) !=2";
            }
            unset($filter['deliv_status']);
        }
        if (isset($filter['expre_status'])) {
            if ($filter['expre_status']=='true') {
                $where .= " AND (print_status & 4) =4";
            }else{
                $where .= " AND (print_status & 4) !=4";
            }
            unset($filter['expre_status']);
        }

        return parent::_filter($filter,$tableAlias,$baseWhere).$where;
    }

    function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'delivery_bn'=>app::get('base')->_('发货单号'),
            'order_bn'=>app::get('base')->_('订单号'),
            'member_uname'=>app::get('base')->_('用户名'),
            'ship_name'=>app::get('base')->_('收货人'),
            'ship_tel_mobile'=>app::get('base')->_('联系电话'),
            'product_bn'=>app::get('base')->_('货号'),
            'product_barcode'=>app::get('base')->_('条形码'),
            'delivery_ident'=>app::get('base')->_('打印批次号'),
            'outer_delivery_bn'=>app::get('base')->_('外部发货单号'),
            'logi_no_ext'=>app::get('base')->_('物流单号'),
        );

        return array_merge($childOptions,$parentOptions);
    }

    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){

        $data = app::get('wms')->model('delivery')->getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end);

        return $data;
    }

    public function getPrimaryIdsByCustom($filter){

        $data = app::get('wms')->model('delivery')->getPrimaryIdsByCustom($filter);
        return $data;
    }
    public function disabled_export_cols(&$cols){

        $cols['order_bn'] = array(
            'type' => 'varchar(32)',
            'required' => true,
            'label' => '订单号',
            'editable' => false,
        );
        $cols['logi_no'] = array(
            'type' => 'varchar(50)',
            'default' => '0',
            'label' => '物流单号',
            'editable' => false,
        );
        $cols['tax_no'] = array(
            'type' => 'varchar(50)',
            'label' => '发票号',
            'editable' => false,
        );
        $cols['custom_mark'] = array(
            'type' => 'longtext',
            'required' => true,
            'label' => '买家留言',
            'editable' => false,
        );
        $cols['mark_text'] = array(
            'type' => 'longtext',
            'required' => true,
            'label' => '客服备注',
            'editable' => false,
        );
        unset($cols['is_protect'],  $cols['column_ident'],$cols['last_modified'],$cols['memo'],$cols['column_status'],$cols['column_process_status'],$cols['column_print_status'],$cols['column_create'],$cols['column_beartime'],$cols['column_deliveryNumInfo'],$cols['column_content'],$cols['delivery_group'],$cols['sms_group'],$cols['ship_email']);

    }

}
